<template>
  <zc-card>
    <avue-crud
      ref="crud"
      :option="option"
      :page.sync="page"
      :table-loading="loading"
      @on-load="getList"
      @row-update="rowUpdate"
      :upload-after="uploadAfter"
      @row-save="rowSave"
      @row-del="rowDel"
      @refresh-change="refreshChange"
      @search-reset="searchChange"
      @search-change="searchChange"
      v-model="form"
      :data="data"
      :permission="permission"
    >
      <template slot="state" slot-scope="scope">
        <el-tag v-if="scope.row.state == 1">启用</el-tag>
        <el-tag type="danger" v-if="scope.row.state == 2">禁用</el-tag>
      </template>
      <template slot="is_tabbar" slot-scope="scope">
        <el-tag v-if="scope.row.is_tabbar == 1">{{
          scope.row.$is_tabbar
        }}</el-tag>
        <el-tag type="danger" v-if="scope.row.is_tabbar == 2">{{
          scope.row.$is_tabbar
        }}</el-tag>
      </template>
      <!-- <template slot-scope="scope" slot="menu">
        <el-button type="primary" :size="scope.size" @click.stop="handleEdit(scope.row,scope.index)">编辑</el-button>
        <el-button type="danger" :size="scope.size" @click.stop="handleDel(scope.row,scope.index)">删除</el-button>
      </template>-->
    </avue-crud>
  </zc-card>
</template>

<script>
import { mapGetters } from "vuex";
import { getList, update, add, del } from "@/api/admin/wxpage.js";
import option from "./wxpage-options.js";

export default {
  name: "wxpage",
  data() {
    return {
      permission: {
        addBtn: false,
        editBtn: false,
        delBtn: false,
      }, //权限
      page: {
        currentPage: 1,
        pageSize: 10,
      },
      form: {},
      params: {},
      loading: false,
      data: [],
      option: {},
    };
  },
  created() {
    const role = this.$defaultRoleStatus;
    this.permission.addBtn = this.zcbtn[this.$options.name + "_add"] || role;
    this.permission.editBtn = this.zcbtn[this.$options.name + "_edit"] || role;
    this.permission.delBtn = this.zcbtn[this.$options.name + "_del"] || role;
    this.option = option(this);
  },
  mounted() {

  },
  computed: {
    ...mapGetters(["zcbtn"]),
  },
  methods: {
    // 自定义删除
    handleDel(row, index) {
      this.$refs.crud.rowDel(row, index);
    },
    // 自定义编辑
    handleEdit(row, index) {
      this.$refs.crud.rowEdit(row, index);
    },
    uploadAfter(res, done) {
      done();
    },
    getList() {
      this.loading = true;
      const data = Object.assign(
        // {
        //   page: this.page.currentPage,
        //   limit: this.page.pageSize,
        // },
        this.params
      );
      this.data = [];

     return getList(data).then((res) => {
        // console.log("页面列表", res);
        this.loading = false;
        this.page.total = res.num;
        this.data = res.data;
        return Promise.resolve(true)
      });
    },
    rowSave(row, done, loading) {
      // console.log("上传数据", row);
      // return;
      add(Object.assign(row))
        .then(() => {
          this.$message.success("新增成功");
          done();
          this.getList();
        })
        .catch(() => {
          loading();
        });
    },
    rowUpdate(row, index, done, loading) {
      update(
        Object.assign(
          {
            role_id: row.role_name,
          },
          row
        )
      )
        .then(() => {
          this.$message.success("修改成功");
          done();
          this.getList();
        })
        .catch(() => {
          loading();
        });
    },
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return del(row.id);
        })
        .then(() => {
          this.$message.success("删除成功");
          this.getList();
        });
    },
     async searchChange(params, done) {
      // console.log(params);
      if (params.add_time && params.add_time.length > 0) {
        params.start_time = params.add_time[0].getTime() / 1000;
        params.end_time = params.add_time[1].getTime() / 1000;
      }

      if (done) done();
      this.params = params;
      this.page.currentPage = 1;
      await this.getList();
      this.$message.success("搜索成功");
    },
    async refreshChange() {
      await this.getList();
      this.$message.success("刷新成功");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>